package com.oy.guli.teacher.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oy.guli.teacher.entity.UserActiveDTO;
import com.oy.guli.teacher.mapper.UserActiveMapper;
import com.oy.guli.teacher.service.UserActiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.redis.core.RedisTemplate;


import java.util.List;
import java.util.Stack;
import java.util.HashMap;

import java.util.Map;


@Service
public class UserActiveServiceImpl extends ServiceImpl<UserActiveMapper, UserActiveDTO> implements UserActiveService {

    @Autowired
    private UserActiveMapper userActiveMapper;




    private static final int STACK_CAPACITY = 10; // 栈的固定长度
    private Stack<String> userandcourseStack = new Stack<>();


    @Override
    public List<UserActiveDTO> listAllUserActive() {
        return userActiveMapper.listAllUserActive();
    }

    @Override
    @Transactional
    public boolean saveUserActive(UserActiveDTO userActiveDTO) {
        String userandcourse = userActiveDTO.getUserId() + ":" + userActiveDTO.getCourseId();
        if (userandcourseStack.size() < STACK_CAPACITY) {
            userandcourseStack.push(userandcourse);
            //入栈之后如果栈满
            if (userandcourseStack.size() == STACK_CAPACITY) {
                Map<String, Integer> occurrences = countOccurrences(userandcourseStack);
                // 输出每个字符串出现的次数
                for (Map.Entry<String, Integer> entry : occurrences.entrySet()) {
                    System.out.println("字符串 \"" + entry.getKey() + "\" 出现的次数: " + entry.getValue());
                    UserActiveDTO userActiveDTO1 = new UserActiveDTO();
                    userActiveDTO1.setUserId(entry.getKey().split(":")[0]);
                    userActiveDTO1.setCourseId(entry.getKey().split(":")[1]);
                    int clickcount = entry.getValue();
                    // 1.先判断数据库中是否存在当前用户的浏览记录
                    int rows = this.userActiveMapper.countUserActive(userActiveDTO);
                    int saveRows = 0;
                    int updateRows = 0;
                    // 2.不存在就添加
                    if (rows < 1) { // 不存在
                        userActiveDTO.setClickCount(clickcount); // 不存在记录的话那肯定是第一次访���，那点击量肯定是1
                        saveRows = this.userActiveMapper.saveUserActive(userActiveDTO);
                    } else { // 已经存在
                        // 3.存在则先把当前用户对的点击量取出来+1
                        int clickcount_befor = this.userActiveMapper.getHitsByUserActiveInfo(userActiveDTO);
                        // 4.然后在更新用户的浏览记录
                        clickcount = clickcount + clickcount_befor;
                        userActiveDTO.setClickCount(clickcount);
                        updateRows = this.userActiveMapper.updateUserActive(userActiveDTO);
                    }

                }

            }
        }


//        boolean flag = false;
//        // 1.先判断数据库中是否存在当前用户的浏览记录
//        int rows = this.userActiveMapper.countUserActive(userActiveDTO);
//        int saveRows = 0;
//        int updateRows = 0;
//        // 2.不存在就添加
//        if (rows < 1) { // 不存在
//            userActiveDTO.setClickCount(1); // 不存在记录的话那肯定是第一次访���，那点击量肯定是1
//            saveRows = this.userActiveMapper.saveUserActive(userActiveDTO);
//        } else { // 已经存在
//            // 3.存在则先把当前用户对的点击量取出来+1
//            int clickcount = this.userActiveMapper.getHitsByUserActiveInfo(userActiveDTO);
//            // 4.然后在更新用户的浏览记录
//            clickcount++;
//            userActiveDTO.setClickCount(clickcount);
//            updateRows = this.userActiveMapper.updateUserActive(userActiveDTO);
//        }
//        if (saveRows > 0 || updateRows > 0) {
//            flag = true;
//        }
//        return flag;


        return true;
    }
    public static Map<String, Integer> countOccurrences (Stack < String > stack) {

        Map<String, Integer> occurrencesMap = new HashMap<>();

        // 遍历栈并统计每个字符串的出现次数
        while (!stack.isEmpty()) {
            String element = stack.pop();
            occurrencesMap.put(element, occurrencesMap.getOrDefault(element, 0) + 1);
        }

        return occurrencesMap;
    }


}
